﻿#include "injectorsettingwidget.h"

InjectorSettingWidget::InjectorSettingWidget()
{
    QGridLayout* layout = new QGridLayout();

    QLabel* injSettingTitle = new QLabel(QStringLiteral("润滑机设置"));
    injSettingTitle->setStyleSheet("background-color:#d4d0c8;");
    injSettingTitle->setAlignment(Qt::AlignCenter);

    int s_w = 4, w = 6;
    int head_h = 1, h = 15;
    layout->addWidget(injSettingTitle,        0,      0,         head_h,     s_w);
    layout->addLayout(setup_settingWindow(),  head_h, 0,         h - head_h, s_w);
    layout->addWidget(setup_warnbox(),        0,      s_w,       h,          w-s_w);
    layout->setMargin(0);
    layout->setSpacing(1);
    setLayout(layout);

    connect(settingButton, &QPushButton::clicked, this, &InjectorSettingWidget::showSetting);
    connect(cancelButton,&QPushButton::clicked, this, &InjectorSettingWidget::cancelSetting);

}

InjectorSettingWidget::~InjectorSettingWidget()
{

}

void InjectorSettingWidget::showWarning(const QString &text)
{
    warntext->clear();
    warntext->setText(text+"\n");
}

void InjectorSettingWidget::showSetting()
{
    if(name_ed->text().isEmpty() || pos_ed->text().isEmpty())
        QMessageBox::warning(NULL, QStringLiteral("请选择设备"), QStringLiteral("未选择设备"));
    else
    {
        okButton->setVisible(true);
        cancelButton->setVisible(true);
        settingButton->setVisible(false);

        name_ed->setEnabled(true);
        pos_ed->setEnabled(true);
        comm_period_ed->setEnabled(true);
        inj_period_ed->setEnabled(true);
        inj_vol_ed->setEnabled(true);
        max_cur_ed->setEnabled(true);
        min_vol_ed->setEnabled(true);
    }
}

bool InjectorSettingWidget::checkSetting()
{
    if(name_ed->text().isEmpty() || pos_ed->text().isEmpty())
    {
        //lot of checks
        QMessageBox::warning(NULL, QStringLiteral("设置错误"), QStringLiteral("设备设置不能为空"));
        return false;
    }
    else
    {
        okButton->setVisible(false);
        cancelButton->setVisible(false);
        settingButton->setVisible(true);

        name_ed->setEnabled(false);
        pos_ed->setEnabled(false);
        comm_period_ed->setEnabled(false);
        inj_period_ed->setEnabled(false);
        inj_vol_ed->setEnabled(false);
        max_cur_ed->setEnabled(false);
        min_vol_ed->setEnabled(false);
        return true;
    }
}

void InjectorSettingWidget::cancelSetting()
{
    flush();
    okButton->setVisible(false);
    cancelButton->setVisible(false);
    settingButton->setVisible(true);

    name_ed->setEnabled(false);
    pos_ed->setEnabled(false);
    comm_period_ed->setEnabled(false);
    inj_period_ed->setEnabled(false);
    inj_vol_ed->setEnabled(false);
    max_cur_ed->setEnabled(false);
    min_vol_ed->setEnabled(false);
}

void InjectorSettingWidget::flush()
{
    name_ed->clear();
    pos_ed->clear();
    comm_period_ed->clear();
    inj_period_ed->clear();
    inj_vol_ed->clear();
    max_cur_ed->clear();
    min_vol_ed->clear();
    sta_ll->setText("");
    bat_ll->setText("");
    oil_ll->setText("");

}

QWidget* InjectorSettingWidget::setup_warnbox()
{
    QGridLayout* layout = new QGridLayout();
    QWidget* group = new QGroupBox(QStringLiteral("警告"));
    warntext = new QTextEdit();
    warntext->setText("This is warning...");
    layout->addWidget(warntext);
    group->setLayout(layout);
    return group;
}

QLayout* InjectorSettingWidget::setup_settingWindow()
{
    QHBoxLayout* layout = new QHBoxLayout();

    QGroupBox* infoGroup = new QGroupBox(QStringLiteral("基本信息"));
    QGridLayout* infolay = new QGridLayout();
    QLabel* name_l = new QLabel(QStringLiteral("名称"));
    name_l->setAlignment(Qt::AlignCenter);
    name_ed = new QLineEdit();
    QLabel* pos_l = new QLabel(QStringLiteral("位置"));
    pos_l->setAlignment(Qt::AlignCenter);
    pos_ed = new QLineEdit();
    QLabel* sta_l = new QLabel(QStringLiteral("状态"));
    sta_l->setAlignment(Qt::AlignCenter);
    sta_ll = new QLabel();
    QLabel* bat_l = new QLabel(QStringLiteral("剩余电量(%)"));
    bat_l->setAlignment(Qt::AlignCenter);
    bat_ll = new QLabel();
    QLabel* oil_l = new QLabel(QStringLiteral("剩余油脂(%)"));
    oil_l->setAlignment(Qt::AlignCenter);
    oil_ll = new QLabel();

    infolay->addWidget(name_l,0,0,1,1);
    infolay->addWidget(name_ed,0,1,1,1);
    infolay->addWidget(pos_l,1,0,1,1);
    infolay->addWidget(pos_ed,1,1,1,1);
    infolay->addWidget(sta_l,2,0,1,1);
    infolay->addWidget(sta_ll,2,1,1,1);
    infolay->addWidget(bat_l,3,0,1,1);
    infolay->addWidget(bat_ll,3,1,1,1);
    infolay->addWidget(oil_l,4,0,1,1);
    infolay->addWidget(oil_ll,4,1,1,1);
    infoGroup->setLayout(infolay);

    QGroupBox* settingGroup = new QGroupBox(QStringLiteral("设置选项"));
    QGridLayout* setlay = new QGridLayout();
    QLabel* inj_period_l = new QLabel(QStringLiteral("注脂周期(h)"));
    inj_period_l->setAlignment(Qt::AlignCenter);
    inj_period_ed = new QLineEdit();
    QLabel* comm_period_l = new QLabel(QStringLiteral("通信周期(h)"));
    comm_period_l->setAlignment(Qt::AlignCenter);
    comm_period_ed = new QLineEdit();
    QLabel* inj_vol_l = new QLabel(QStringLiteral("注脂量(ml)"));
    inj_vol_l->setAlignment(Qt::AlignCenter);
    inj_vol_ed = new QLineEdit();
    QLabel* max_cur_l = new QLabel(QStringLiteral("电流上限(mA)"));
    max_cur_l->setAlignment(Qt::AlignCenter);
    max_cur_ed = new QLineEdit();
    QLabel* min_vol_l = new QLabel(QStringLiteral("电压下限值(mV)"));
    min_vol_l->setAlignment(Qt::AlignCenter);
    min_vol_ed = new QLineEdit();
    name_ed->setEnabled(false);
    pos_ed->setEnabled(false);
    comm_period_ed->setEnabled(false);
    inj_period_ed->setEnabled(false);
    inj_vol_ed->setEnabled(false);
    max_cur_ed->setEnabled(false);
    min_vol_ed->setEnabled(false);

    settingButton = new QPushButton(QStringLiteral("设置"));
    okButton = new QPushButton(QStringLiteral("确认"));
    cancelButton = new QPushButton(QStringLiteral("取消"));

    setlay->addWidget(inj_period_l,0,0,1,1);
    setlay->addWidget(inj_period_ed,0,1,1,2);
    setlay->addWidget(comm_period_l,1,0,1,1);
    setlay->addWidget(comm_period_ed,1,1,1,2);
    setlay->addWidget(inj_vol_l,2,0,1,1);
    setlay->addWidget(inj_vol_ed,2,1,1,2);
    setlay->addWidget(max_cur_l,3,0,1,1);
    setlay->addWidget(max_cur_ed,3,1,1,2);
    setlay->addWidget(min_vol_l,4,0,1,1);
    setlay->addWidget(min_vol_ed,4,1,1,2);

    setlay->addWidget(settingButton,5,0,1,1);
    setlay->addWidget(okButton,5,1,1,1);
    setlay->addWidget(cancelButton,5,2,1,1);

    okButton->setVisible(false);
    cancelButton->setVisible(false);

    settingGroup->setLayout(setlay);

    layout->addWidget(infoGroup);
    layout->addWidget(settingGroup);
    layout->setMargin(5);
    layout->setSpacing(10);

    return layout;
}

void InjectorSettingWidget::setInfo(const InjectorData &data)
{
    name_ed->setText(data.name);
    pos_ed->setText(data.position);
    sta_ll->setText(data.status == 0 ? QStringLiteral("正常") : QStringLiteral("异常"));
    bat_ll->setNum(data.battery);
    oil_ll->setNum(data.oil);
    inj_period_ed->setText(QString::number(data.settings.inject_period));
    comm_period_ed->setText(QString::number(data.settings.comm_period));
    inj_vol_ed->setText(QString::number(data.settings.inject_volume));
    max_cur_ed->setText(QString::number(data.settings.max_current));
    min_vol_ed->setText(QString::number(data.settings.min_volt));
}

InjectorSetting InjectorSettingWidget::getSetting()
{
    InjectorSetting settings;
    settings.inject_period = inj_period_ed->text().toInt();
    settings.comm_period = comm_period_ed->text().toInt();
    settings.inject_volume = inj_vol_ed->text().toInt();
    settings.max_current = max_cur_ed->text().toInt();
    settings.min_volt = min_vol_ed->text().toInt();
    return settings;
}
